package com.proactive.webapp.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.proactive.webapp.dao.CourseCommentDAO;
import com.proactive.webapp.dao.DesignCommentDAO;
import com.proactive.webapp.dao.WorksheetCommentDAO;
import com.proactive.webapp.entity.CourseComment;
import com.proactive.webapp.entity.DesignComment;
import com.proactive.webapp.entity.Member;
import com.proactive.webapp.entity.WorksheetComment;

@Controller
public class CommentController {
	ApplicationContext context =  new ClassPathXmlApplicationContext("spring-module.xml");
	
	@RequestMapping(value = "/addComment", method = RequestMethod.POST)
	public ModelAndView addComment(int type, int id, String content, HttpSession session){
		ModelAndView model = new ModelAndView();
		Member loginMember = (Member)session.getAttribute("loginMember");
		if(loginMember != null){
			String loginAccount = loginMember.getAccount();
			if(type == 1){
				CourseCommentDAO courseCommentDAO = (CourseCommentDAO) context.getBean("courseCommentDAO");
				CourseComment courseComment = new CourseComment();
				courseComment.setCid(id);
				courseComment.setContent(content);
				courseComment.setAccount(loginAccount);
				courseCommentDAO.insert(courseComment);
				model.setViewName("redirect:/showCourse?id="+id+"#commentArea");
			}
			else if(type == 2){
				DesignCommentDAO designCommentDAO = (DesignCommentDAO) context.getBean("designCommentDAO");
				DesignComment designComment = new DesignComment();
				designComment.setDid(id);
				designComment.setContent(content);
				designComment.setAccount(loginAccount);
				designCommentDAO.insert(designComment);
				model.setViewName("redirect:/showDesign?id="+id+"#commentArea");
			}
			else if(type == 3){
				WorksheetCommentDAO worksheetCommentDAO = (WorksheetCommentDAO) context.getBean("worksheetCommentDAO");
				WorksheetComment worksheetComment = new WorksheetComment();
				worksheetComment.setWid(id);
				worksheetComment.setContent(content);
				worksheetComment.setAccount(loginAccount);
				worksheetCommentDAO.insert(worksheetComment);
				model.setViewName("redirect:/showWorksheet?id="+id+"#commentArea");
			}
		}
		else
			model.setViewName("redirect:/login");
		return model;
	}

	@RequestMapping(value = "/replyComment", method = RequestMethod.POST)
	public ModelAndView replyComment(int type, int id, int topID, String content, 
			HttpSession session, HttpServletRequest request){
		ModelAndView model = new ModelAndView();
		Member loginMember = (Member)session.getAttribute("loginMember");
		if(loginMember != null){
			String loginAccount = loginMember.getAccount();
			if(type == 1){
				CourseCommentDAO courseCommentDAO = (CourseCommentDAO) context.getBean("courseCommentDAO");
				CourseComment courseComment = new CourseComment();
				courseComment.setCid(id);
				courseComment.setTopCommentID(topID);
				courseComment.setContent(content);
				courseComment.setAccount(loginAccount);
				courseCommentDAO.insertReply(courseComment);
				model.setViewName("redirect:"+request.getHeader("referer")+"#comment"+topID);
			}
			else if(type == 2){
				DesignCommentDAO designCommentDAO = (DesignCommentDAO) context.getBean("designCommentDAO");
				DesignComment designComment = new DesignComment();
				designComment.setDid(id);
				designComment.setTopCommentID(topID);
				designComment.setContent(content);
				designComment.setAccount(loginAccount);
				designCommentDAO.insertReply(designComment);
				model.setViewName("redirect:"+request.getHeader("referer")+"#comment"+topID);
			}
			else if(type == 3){
				WorksheetCommentDAO worksheetCommentDAO = (WorksheetCommentDAO) context.getBean("worksheetCommentDAO");
				WorksheetComment worksheetComment = new WorksheetComment();
				worksheetComment.setWid(id);
				worksheetComment.setTopCommentID(topID);
				worksheetComment.setContent(content);
				worksheetComment.setAccount(loginAccount);
				worksheetCommentDAO.insertReply(worksheetComment);
				model.setViewName("redirect:"+request.getHeader("referer")+"#comment"+topID);
			}
		}
		else
			model.setViewName("redirect:/login");
		return model;
	}
	
	@RequestMapping(value = "/delComment", method = RequestMethod.POST)
	public ModelAndView delComment(int type, int commentID, HttpSession session, HttpServletRequest request){
		ModelAndView model = new ModelAndView();
		Member loginMember = (Member)session.getAttribute("loginMember");
		if(loginMember != null){
			String loginAccount = loginMember.getAccount();
			if(type == 1){
				CourseCommentDAO courseCommentDAO = (CourseCommentDAO) context.getBean("courseCommentDAO");
				CourseComment courseComment = new CourseComment();
				courseComment.setId(commentID);
				courseComment = courseCommentDAO.get(courseComment);
				if(courseComment.getAccount().equals(loginAccount))
					courseCommentDAO.delete(commentID);
				model.setViewName("redirect:"+request.getHeader("referer")+"#commentArea");
			}
			else if(type == 2){
				DesignCommentDAO designCommentDAO = (DesignCommentDAO) context.getBean("designCommentDAO");
				DesignComment designComment = new DesignComment();
				designComment.setId(commentID);
				designComment = designCommentDAO.get(designComment);
				if(designComment.getAccount().equals(loginAccount))
					designCommentDAO.delete(commentID);
				model.setViewName("redirect:"+request.getHeader("referer")+"#commentArea");
			}
			else if(type == 3){
				WorksheetCommentDAO worksheetCommentDAO = (WorksheetCommentDAO) context.getBean("worksheetCommentDAO");
				WorksheetComment worksheetComment = new WorksheetComment();
				worksheetComment.setId(commentID);
				worksheetComment = worksheetCommentDAO.get(worksheetComment);
				if(worksheetComment.getAccount().equals(loginAccount))
					worksheetCommentDAO.delete(commentID);
				model.setViewName("redirect:"+request.getHeader("referer")+"#commentArea");
			}
		}
		else
			model.setViewName("redirect:/login");
		return model;
	}
	
}
